GitHub scrubs the Javascript so you won't be able to see the map from GitHub directly. Go to this link to view the visualization: https://nbviewer.jupyter.org/github/JasonSanchez/dangerous-intersections/blob/master/accidents/NYC%20Accident%20Data%20Exploration.ipynb.
In [2]:
# Import dependencies
import folium
import numpy as np
import pandas as pd
# Load accident data.
accident_data = pd.read_csv('./data/NYPD_Motor_Vehicle_Collisions_sampled.csv')
In [23]:
accident_data.head()
Out[23]:
In [95]:
accident_data.describe()
Out[95]:
In [15]:
# Num rows in data.
print(accident_data.count())
In [3]:
# Map data.
# Starting coordinates to load map view.
NYC_coordinates = (40.7142700, -74.0059700)
# Create Map object.
map = folium.Map(location=NYC_coordinates,
zoom_start=12)
# Plot accidents.
# Limit number of points to plot for testing.
MAX_RECORDS = 1000
marker_cluster = folium.MarkerCluster().add_to(map)
for row in accident_data[0:MAX_RECORDS].iterrows():
# Only plot point if lat/long is available.
if (not np.isnan(row[1]['LATITUDE']) and not np.isnan(row[1]['LONGITUDE'])):
accident_metadata = """
<ul>
<li><strong>On street</strong>: {0}</li>
<li><strong>Cross street</strong>: {1}</li>
<li><strong>Reason</strong>: {2}</li>
</ul>""".format(
str(row[1]['ON STREET NAME']), str(row[1]['CROSS STREET NAME']),
str(row[1]['CONTRIBUTING FACTOR VEHICLE 1']))
iframe = folium.element.IFrame(html=accident_metadata, width=250, height=100)
popup = folium.Popup(iframe, max_width=2650)
folium.Marker(
location = [row[1]['LATITUDE'], row[1]['LONGITUDE']],
icon = folium.Icon(color='red', icon='asterisk'),
popup=popup).add_to(marker_cluster)
map
Out[3]:
In [4]:
# Save html version of map.
map.save('accidents_map.html')
In [ ]: